﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;
using BUS;

namespace GUI
{
    public partial class frmHoaDonBar : DevComponents.DotNetBar.Office2007Form
    {
        public frmHoaDonBar()
        {
            InitializeComponent();
        }

        public bool loadXongForm = false;
        public bool hoanTat = false;
        public List<SANPHAM> arrSP = SanPhamBUS.layDSSP();
        public KHACHHANG kh = new KHACHHANG();
        double tiLeGiamGia = 0;
        public NHANVIEN nv = new NHANVIEN();

        public void khoiTao()
        {
            lblNhanVien.Text = nv.TenNV;
            lblNgayGio.Text = DateTime.Now.ToString();
            txtMaHD.Text = (HoaDonSPBUS.layMaHDLonNhat() + 1).ToString();
            List<KHACHHANG> arrKH = KhachHangBUS.layDSKH();
            cbbMaKH.DataSource = arrKH;
            cbbMaKH.ValueMember = "MaKH";
            cbbMaKH.DisplayMember = "MaKH";

            kh = arrKH[0];
            txtTenKH.Text = kh.TenKH;

            cbbSP.DataSource = arrSP;
            cbbSP.DisplayMember = "TenSP";
            cbbSP.ValueMember = "MaSP";

            DataTable dt = new DataTable();
            dt.Columns.Add("Mã");
            dt.Columns.Add("Sản phẩm");
            dt.Columns.Add("Đơn giá");
            dt.Columns.Add("Số lượng");
            dt.Columns.Add("Thành tiền");

            dt.Columns[0].ReadOnly = true;
            dt.Columns[1].ReadOnly = true;
            dt.Columns[2].ReadOnly = true;
            dt.Columns[4].ReadOnly = true;

            grvCTHD.Refresh();
            grvCTHD.DataSource = dt;
            

            txtSoLuong.Text = "";
            txtTongTien.Text = "";
        }


        private void frmHoaDonBar_Load(object sender, EventArgs e)
        {
            khoiTao();
            loadXongForm = true;
        }

        private void cbbMaKH_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (loadXongForm)
            {
                kh = KhachHangBUS.layKH(int.Parse(cbbMaKH.SelectedValue.ToString()));
                txtTenKH.Text = kh.TenKH;
            }
        }

        private void btnThemCTHD_Click(object sender, EventArgs e)
        {
            if (txtSoLuong.Text != "")
            {
                DataTable dt = ((DataTable)grvCTHD.DataSource).Copy();
                DataRow dr = dt.NewRow();
                int soLuong = int.Parse(txtSoLuong.Text);
                // decimal giaTien = arrDSGV[int.Parse(cbbVe.SelectedItem.ToString())].GiaTien;

                SANPHAM sp = SanPhamBUS.laySP(int.Parse(cbbSP.SelectedValue.ToString()));
                //decimal giaTien = ve.GiaTien;
                dr[0] = sp.MaSP;
                dr[1] = sp.TenSP;
                dr[2] = sp.GiaSP;
                dr[3] = soLuong.ToString();
                dr[4] = (soLuong * sp.GiaSP).ToString();

                dt.Rows.Add(dr);
                grvCTHD.DataSource = null;
                grvCTHD.DataSource = dt;

                tinhTongTam(dt);
            }
            else
            {
                MessageBox.Show("Bạn đưa nhập số lượng sản phẩm");
                txtSoLuong.Focus();
            }
        }

        public void tinhTongTam(DataTable dt)
        {
            double tongTien = 0;
            foreach (DataRow row in dt.Rows)
            {
                tongTien += double.Parse(row[4].ToString());
            }
            txtTongTien.Text = tongTien.ToString();
        }

        private void grvCTHD_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            int row = grvCTHD.CurrentCell.RowIndex;
            DataTable dt = ((DataTable)grvCTHD.DataSource).Copy();

            int col = grvCTHD.CurrentCell.ColumnIndex;

            //MessageBox.Show(grvCTHD.Rows[row].Cells[col].Value.ToString());
        }

        private void btnLapHD_Click(object sender, EventArgs e)
        {
            DataTable dt = ((DataTable)grvCTHD.DataSource).Copy();
            if (dt.Rows.Count != 0)
            {
                //set up new order and insert
                HOADON_SP hoaDonSP = new HOADON_SP();
                if (kh.MaKH != 1) { hoaDonSP.MaKH = kh.MaKH; }

                hoaDonSP.NgayLap = DateTime.Now;
                hoaDonSP.TriGia = decimal.Parse(txtTongTien.Text);
                hoaDonSP.NhanVienTao = nv.MaNV;
                hoaDonSP.TrangThai = true;

                bool result = HoaDonSPBUS.themHoaDonSP(hoaDonSP);
                if (result)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        CTHD_SP cthd = new CTHD_SP();
                        cthd.MaHDSP = hoaDonSP.MaHDSP;
                        cthd.MaSP = int.Parse(row[0].ToString());
                        cthd.SoLuong = int.Parse(row[3].ToString());
                        cthd.GiaSP= decimal.Parse(row[2].ToString());
                        hoaDonSP.CTHD_SP.Add(cthd);
                    }
                    result = HoaDonSPBUS.capNhatHoaDon(hoaDonSP);

                    if (result)
                    {
                        MessageBox.Show("Lập hóa đơn thành công");
                        hoanTat = true;
                        khoiTao();
                    }
                    else
                    {
                        MessageBox.Show("Lập hóa đơn không thành công. Vui lòng thử lại.");
                        hoanTat = true;
                        khoiTao();
                    }
                }
                else
                {
                    MessageBox.Show("Lập hóa đơn không thành công. Vui lòng thử lại.");
                    hoanTat = true;
                    khoiTao();
                }
            }
            else
            {
                MessageBox.Show("Chưa nhập vé");
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            if (!hoanTat)
            {
                DialogResult dialogResult = MessageBox.Show("Bạn có thực sự muốn tắt màn hình lập hóa đơn?", "Xác nhận", MessageBoxButtons.YesNo);
                if (dialogResult == DialogResult.Yes)
                {
                    this.Close();
                }
            }
            else
                this.Close();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (!hoanTat)
            {
                DialogResult dialogResult = MessageBox.Show("Hóa đơn chưa lập xong. Bạn có thực sự muốn lập hóa đơn mới?", "Xác nhận", MessageBoxButtons.YesNo);
                if (dialogResult == DialogResult.Yes)
                {
                    khoiTao();
                }
            }
            else
                khoiTao();
        }
    }
}
